package cn.iocoder.ny.module.mall.controller.admin.order;

import cn.iocoder.ny.module.mall.controller.admin.order.vo.MallOrderDetailsVo;
import cn.iocoder.ny.module.mall.controller.admin.order.vo.MallOrderVo;
import cn.iocoder.ny.module.mall.domain.MallOrder;
import cn.iocoder.ny.module.mall.domain.vo.MallWeChatPayVo;
import cn.iocoder.ny.module.mall.service.IMallOrderService;
import cn.iocoder.ny.module.mall.utils.wxpay.AjaxResult;
import cn.iocoder.ny.module.mall.utils.wxpay.ResultModel;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
import com.wechat.pay.java.service.payments.jsapi.model.Payer;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;


/**
 * 商城订单
 * @author
 */
@RestController
@RequestMapping("/order")
public class MallOrderController
{
    public static String appId ="wx59d2c278e4e88df1";
    public static String merchantId = "1680551706";
    public static String privateKeyPath = "E:\\tuijingutil\\appdemo1\\pemfies\\apiclient_key_new.pem";
    public static String merchantSerialNumber = "183169603A7ED47E743FC538BA8EDB7E279FA131";
    public static String apiV3Key = "dnm5cz1mvyvfwkp4ngx45qtbv1zqrcbj";
    String callurl = "https://2rz24qd15207.vicp.fun/api/ordercallback";







    private static final Logger logger = LoggerFactory.getLogger(MallOrderController.class);


    @Autowired
    private IMallOrderService mallOrderService;

    /**
     * 新增订单
     * @param
     * @return
     */
    @PostMapping("/addOrder222")
    public CommonResult<Boolean> addOrder(@RequestBody  HashMap<String,Object> moandmodetail){


        mallOrderService.addOrder(moandmodetail);


        return success(true);
    }


    @PostMapping("/addOrder")
    public AjaxResult submitOrder(@RequestBody  HashMap<String,Object> moandmodetail){
        if(moandmodetail.get("productid") == null){
            return AjaxResult.error("购买的产品id空");
        }
        if(moandmodetail.get("quantity") == null){
            return AjaxResult.error("购买的数量不能为空");
        }
        return AjaxResult.success( mallOrderService.addOrder(moandmodetail));
    }


    //
    @PostMapping("/newpay")
    public AjaxResult addOrderFake(@RequestBody  HashMap<String,Object> moandmodetail){

        return AjaxResult.success( mallOrderService.newpay(moandmodetail));
    }








    //去支付
    @PostMapping("/goToPay")
    public AjaxResult goToPay(@RequestBody  HashMap<String,Object> moandmodetail){
        if(moandmodetail.get("orderid") == null){
            return AjaxResult.error("去支付的订单id不能为空");
        }

        return AjaxResult.success( mallOrderService.goToPay(moandmodetail));
    }



    /**
     * 更新订单信息
     * @param order
     * @return
     */

    @PostMapping("/updateOrder")
    public CommonResult<Boolean> updateOrder(@RequestBody MallOrderDetailsVo order){
        //控制器 只是开发测试用
        //服务器 技术更新，业务更新
        //
        return success(true);
    }


    @PostMapping("/deleteOrder")
    public CommonResult<Boolean> deleteOrder(@RequestBody MallOrderDetailsVo order){
        //todo
        return success(true);
    }

    @PostMapping("/getOrderList")
    public CommonResult<PageResult<MallOrder>> getOrderList(@RequestBody @Validated(MallOrderDetailsVo.Query.class) MallOrderDetailsVo ordervo){
        //getOrderList
        MallOrderVo mallOrderVo = new MallOrderVo();
        mallOrderVo.setTenantId(ordervo.getTenantId());
        PageResult<MallOrder> orderList = mallOrderService.getMallOrderpage(mallOrderVo);

        return success(orderList);
    }

//
//    @RequestMapping("/getOrderListMap")
//    public Object getPage(Model model, @RequestParam(defaultValue = "1") Integer page){
//        mallOrderService.getMallOrderPageMap();
//    }


    @RequestMapping("/getOrderListMap")
    public CommonResult<PageResult<HashMap<String,Object>>> getOrderListMap(@RequestBody @Validated(MallOrderDetailsVo.Query.class) MallOrderDetailsVo ordervo){
        MallOrderVo mallOrderVo = new MallOrderVo();
        mallOrderVo.setTenantId(ordervo.getTenantId());
        PageResult<HashMap<String,Object>> orderList  =  mallOrderService.getMallOrderPageMap(mallOrderVo);
        return success(orderList);
    }

    //用户的这个店的所有订单
    @PostMapping("/getMallOrderList")
    public List<HashMap<String,Object>> getMallOrderList(@RequestBody  MallOrderDetailsVo order){
        MallOrderVo mallOrderVo = new MallOrderVo();
        //当前登录的用户
        Long tenantId = getLoginUser().getTenantId();
        Long userid = getLoginUser().getId();

        mallOrderVo.setTenantId(order.getTenantId());
        List<HashMap<String,Object>> list = mallOrderService.getMallOrderList(mallOrderVo,userid);
        return list;
    }



    //这个测试ok
    //非鱼道写法
    @PostMapping("/selectPageByMap")
    public IPage<HashMap<String,Object>> selectPageByMap(@RequestBody  HashMap<String,Object> hashMap){
        MallOrderVo mallOrderVo = new MallOrderVo();
        //当前登录的用户
        Long userid = getLoginUser().getId();
        Long t = Long.valueOf(hashMap.get("tenantId").toString());//前端传
        mallOrderVo.setTenantId(t);
        //
        HashMap<String,Object> hashMapform = new HashMap<>();

        IPage<HashMap<String, Object>> ipage = mallOrderService.selectPageByMap(hashMap,userid);
        return ipage;
    }

    //控制器包装一下，鱼道写法

    /**
     * 入参
     *
     *
     * "userid":123,
     *     "tenantid":1998,
     *     "status":0
     *       "currentpage":1,
     *      "pagesize":10,
     * }
     * @param hashMap
     * @return
     */
    @PostMapping("/selectPageByMapYD")
    public CommonResult<PageResult<HashMap<String,Object>>> selectPageUseMapPR (@RequestBody  HashMap<String,Object> hashMap){
        //当前登录的用户
        Long userid = getLoginUser().getId();
        Long t = Long.valueOf(hashMap.get("tenantid").toString());//前端传
        Long currentpage = Long.valueOf(hashMap.get("currentpage").toString());


        IPage<HashMap<String, Object>> ipage = mallOrderService.selectPageByMap(hashMap,userid);
        PageResult pageResult = new PageResult<>();
        pageResult.setList(ipage.getRecords());
        pageResult.setTotal(ipage.getTotal());
        return success(pageResult);
    }

    /**
     *
     * @param hashMap
     * @return
     */
    //selectInfoByMapYD
    @PostMapping("/selectInfoByMapYD")
    public CommonResult<HashMap<String,Object>> selectInfoByMapYD (@RequestBody  HashMap<String,Object> hashMap){
        String json = JSON.toJSON(hashMap).toString();
        System.out.println(json);
        HashMap<String, Object> hashmap = mallOrderService.selectMapByMap(hashMap);
        return success(hashmap);
    }

    //卖家店长看所有混合订单（自己在自己的定铺看的订单）
    @PostMapping("/selectPageByMapYDForSeller")
    public CommonResult<PageResult<HashMap<String,Object>>> selectPageByMapYDForSeller (@RequestBody  HashMap<String,Object> hashMap){
        //当前登录的用户
        //Long userid = getLoginUser().getId();
        //简单逻辑。不在意谁的订单； 其实可以查到当前登录的人是否有店铺。但是不关注值要查所有这个定铺的订单
        Long t = Long.valueOf(hashMap.get("tenantid").toString());//前端传
        Long currentpage = Long.valueOf(hashMap.get("currentpage").toString());

        IPage<HashMap<String, Object>> ipage = mallOrderService.selectPageByMapYDForSeller(hashMap);
        PageResult pageResult = new PageResult<>();
        pageResult.setList(ipage.getRecords());
        pageResult.setTotal(ipage.getTotal());
        return success(pageResult);
    }


    //todo 查订单的核销员

    /**
     * 根据订单号查 核销员的信息
     * @param hashMap
     * @return
     */
    @PostMapping("/findVeriUser")
    public CommonResult<HashMap<String,Object>> findVeriUser(@RequestBody  HashMap<String,Object> hashMap){
        String json = JSON.toJSON(hashMap).toString();
        System.out.println(json);
        Long orderid =  Long.valueOf(hashMap.get("orderid").toString());
        HashMap<String, Object> hashmap = mallOrderService.findVeriUser(orderid);
        //mallOrderService.lambdaQuery().eq()

        return success(hashmap);
    }

    //平台申请  和驳回
    //appForRefund
    @PostMapping("/appForRefund")
    public CommonResult<HashMap<String,Object>> appForRefund(@RequestBody  HashMap<String,Object> hashMap){
        //根据订单号修改状态
        Long orderid =  Long.valueOf(hashMap.get("orderid").toString());
        MallOrder mallOrder = new MallOrder();
        String reason = hashMap.get("reason").toString();
        mallOrder.setId(orderid);
        mallOrder.setStatus(3);//退款中
        mallOrder.setReason(reason);
        boolean r = mallOrderService.updateById(mallOrder);
        HashMap<String, Object> result = new HashMap<>();
        result.put("flag",r);
        return success(result);
    }

    @PostMapping("/cancelApp")
    public CommonResult<HashMap<String,Object>> cancelApp(@RequestBody  HashMap<String,Object> hashMap){
        //根据订单号修改状态
        Long orderid =  Long.valueOf(hashMap.get("orderid").toString());
        Integer status  =  Integer.valueOf(hashMap.get("status").toString());
        MallOrder mallOrder = new MallOrder();
        mallOrder.setId(orderid);
        mallOrder.setStatus(status);//待核销
        boolean r = mallOrderService.updateById(mallOrder);
        HashMap<String, Object> result = new HashMap<>();
        result.put("flag",r);
        return success(result);
    }



    //disagreeToRefund 驳回
    @PostMapping("/disagreeToRefund")
    public CommonResult<HashMap<String,Object>> disagreeToRefund(@RequestBody  HashMap<String,Object> hashMap){
        Long orderid =  Long.valueOf(hashMap.get("orderid").toString());
        MallOrder mallOrder = new MallOrder();
        mallOrder.setId(orderid);
        mallOrder.setStatus(5);//驳回；当然因为已经付款了。仍然是可以可以去核销的
        boolean r = mallOrderService.updateById(mallOrder);
        HashMap<String, Object> result = new HashMap<>();
        result.put("flag",r);
        return success(result);
    }





}
